<template>
<div>
    <Form ref="EditForm" :model="FormData"  label-position="top">
        <Row :gutter="16">
            <Col :xs="24" :md="8">
                <FormItem label="企业编码" prop="OS_Code" :rules="{required: true, message:'不能为空', trigger: 'blur'}">
                    <Input v-model="FormData.OS_Code"></Input>
                </FormItem>
            </Col>
            <Col :xs="24" :md="8">
            <FormItem label="合作伙伴名称" prop="OS_Name" :rules="{required: true, message:'不能为空', trigger: 'blur'}">
                <Input v-model="FormData.OS_Name"></Input>
            </FormItem>
            </Col>
            <Col :xs="24" :md="8">
                <FormItem label="所属系统" prop="OS_PlatFormID" :rules="{required: true,type:'number', message:'不能为空', trigger: 'change'}">
                    <Select v-model="FormData.OS_PlatFormID" clearable >
                         <Option v-for="item in PlatformItems" :value="item.IP_ID" :key="item.IP_ID">{{ item.IP_Name }}</Option>
                    </Select>
                </FormItem>
                </Col>

                <Col :xs="24" :md="8">
                <FormItem label="选择系统套餐" prop="OS_PlatformRuleID" :rules="{required: true,type:'number', message:'不能为空', trigger: 'change'}">
                    <Select v-model="FormData.OS_PlatformRuleID" clearable>
                         <Option v-for="item in _PlatformItems" :value="item.RuleID" :key="item.RuleID">{{ item.RuleName }}</Option>
                    </Select>
                </FormItem>
                </Col>

            <Col :xs="24" :md="8">
                <FormItem label="所属企业" prop="OS_PartnersID">
                    <PartnersSelect :multiple="false"  v-model="FormData.OS_PartnersID"    placeholder='请选择'></PartnersSelect>
                </FormItem>
            </Col>
            <!--<Col :xs="24" :md="8">-->
            <!--<FormItem label="负责人" prop="OS_Admin_UserID" :rules="{required: false, message:'不能为空', trigger: 'blur'}">-->
                <!--<Input v-model="FormData.OS_Admin_UserID"></Input>-->
            <!--</FormItem>-->
            <!--</Col>-->

             <Col :xs="24" :md="8">
            <FormItem label="所属区域" prop="OS_AreaCode">
                <DictSelect :multiple="false"  v-model="FormData.OS_AreaCode"  label="area"  placeholder='请选择'></DictSelect>
            </FormItem>
            </Col>



           <Col :xs="24" :md="8">
            <FormItem label="地址" prop="OS_Addr" :rules="{required: false, message:'不能为空', trigger: 'blur'}">
                <Input v-model="FormData.OS_Addr"></Input>
            </FormItem>
            </Col>

<!--
            <Col :xs="24" :md="8">
            <FormItem label="可见字典表设置" prop="IP_Dict" :rules="{required: true, type: 'array', message:'不能为空', trigger: 'change'}">
                <DictSelect :multiple="true"  v-model="FormData.IP_Dict"  label=""  placeholder='请选择'></DictSelect>
            </FormItem>
            </Col> -->


            <Col :xs="24" :md="24">
            <FormItem label="备注" prop="IP_Desc">
                <Input v-model="FormData.OS_Desc"></Input>
            </FormItem>
            </Col>

        </Row>
        <Row>
            <Col>
            <FormItem>
                <Button type="primary" @click="handleSubmit()">提交</Button>
                <Button   @click="handleReset()" style="margin-left: 8px">重置</Button>
            </FormItem>
            </Col>
        </Row>

    </Form>
</div>
</template>

<script>
import then from 'thenjs';
import moment from 'moment';
import AppFrame from '../../../libs/app-frame';
import DictSelect from '../../../components/dict-select';
import PartnersSelect from '../../../components/partners-select';
export default {
     components: {
        DictSelect,
         PartnersSelect
    },
    data: function() {
        return {
            State: { //数据加载状态
                IsLoadingData: false
            },
            PlatformRuleItems:[],
            PlatformItems:[],
            FormData: {
                OS_ID:0,
                OS_Code:'',
                OS_Name:'',
                OS_Admin_UserID:'',
                OS_Addr:'',
                OS_Desc:'',
                OS_PlatformRuleID:0,
                OS_PlatFormID:0,
                OS_ParentID:0
            },
            Query: {}
        }
    },
    created: function() {
        this.Query = this.$route.query;
        this.getFormData();
    },
    computed:{
        _PlatformItems:function(){

            let self=this;
            let res=[];
            if(this.FormData.OS_PlatFormID>0){
                for(let i=0;i<self.PlatformRuleItems.length;i++){
                    if(self.PlatformRuleItems[i].RulePlatformID==self.FormData.OS_PlatFormID){
                        res.push(self.PlatformRuleItems[i]);
                    }
                }
                return res;
                alert(JSON.stringify(res));
            }
            return [];
        }
    },
    methods: {
        getFormData() {
            let self = this;
            self.State.IsLoadingData = true;
            let postData={
                 id: self.Query.id
            };

            AppFrame.Http.Post("/manage-api/system/partners/getItem", postData, function(err, ret) {
                self.State.IsLoadingData = false;
              console.log(ret);
                if (ret && ret.Result == 1) {
                    self.FormData = ret.Data.Model;

                    // let IP_Dict=ret.Data.Model.IP_Dict?JSON.parse(ret.Data.Model.IP_Dict):[];
                    // let _ipDict=[];
                    // for(var i in IP_Dict){
                    //     if(IP_Dict[i]){
                    //         IP_Dict[i].map(function(it){
                    //             _ipDict.push(`${i}-${it}`);
                    //         })
                    //     }
                    // }
                    //
                    //  self.FormData.IP_Dict=_ipDict;
                     self.PlatformRuleItems=ret.Data.PlatformRuleItems;
                    self.PlatformItems=ret.Data.PlatformItems;
                } else {
                    self.$Message.error('数据加载失败');
                }
            });
        },
        handleSubmit() {
            let self=this;

            self.$refs["EditForm"].validate((valid) => {
                if (valid) {

                    // let IP_Dict={};
                    // self.FormData.IP_Dict.map(function(item){
                    //
                    //     let _it=item.split('-');
                    //      if(!IP_Dict[_it[0]]){
                    //          IP_Dict[_it[0]]=[];
                    //      }
                    //      IP_Dict[_it[0]].push(_it[1]);
                    // });
                    // self.FormData.IP_Dict=JSON.stringify(IP_Dict);

                    AppFrame.Http.Post("/manage-api/system/partners/edit",self.FormData,function(err,ret){
                        if(ret && ret.Result==1){
                            self.$Message.success('保存成功');
                            self.$router.go(-1);
                        }else{
                            self.$Message.error(ret.Msg);
                        }
                    })
                } else {
                    self.$Message.error('表单验证失败!');
                }
            });
        },
        handleReset() {
            this.$refs["EditForm"].resetFields();
        },

        onPlatformChange:function(val){
            let self=this;

            this.PlatformRuleItems.splice(0,this.PlatformRuleItems.length);
            if(!val) return;
            AppFrame.Http.Post("/manage-api/system/platform-rule/getItemByPlatformId",{id:val},function(err,ret){
                console.log(ret)
                if(ret && ret.Result==1){
                    self.PlatformRuleItems=ret.Data;
                }
            })
        },


    }
}
</script>

<style scoped>

.img_b{cursor: pointer;width:177px;height: 100px;overflow: hidden;border:dotted 1px #ddd;background-position: center center;background-size:cover}
</style>
